package com.cdbkj.cloud.pos.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.androidyang.library.http.okhttp.bean.HttpResult;
import com.androidyang.library.http.okhttp.listener.HttpClientHandler;
import com.cdbkj.cloud.pos.R;
import com.cdbkj.cloud.pos.bean.RegInfoForm;
import com.cdbkj.cloud.pos.http.HttpRequest;
import com.cdbkj.cloud.pos.bean.JsonResult;
import com.cdbkj.cloud.pos.utils.Tools;

import java.util.Map;

public class ConfirmRegFragment extends Fragment implements View.OnClickListener, HttpClientHandler {
    private static final String ARG_USERID = "userId";
    private static final String ARG_REGTYPEID = "regTypeId";
    private static final String ARG_REGTYPE = "regType";
    private static final String ARG_DATE = "regDate";
    private static final String ARG_DEPTID = "departmentId";
    private static final String ARG_DEPTNAME = "departmentName";
    private static final String ARG_SCHDULEID = "scheduleId";
    private static final String ARG_CILINICCODE = "clinicCode";
    private static final String ARG_DOCTORID = "doctorId";
    private static final String ARG_DOCTORNAME = "doctorName";
    private static final String ARG_CARDTYPEID = "cardTypeId";
    private static final String ARG_CARDNO = "cardNo";
    private static final String ARG_COST = "totalCost";
    private static final String ARG_PATIENTID = "patientId";
    private static final String ARG_PATIENTNAME = "patientName";
    private static final String ARG_IDCARDNO = "idCardNo";

    private RegInfoForm regInfoForm;
    private Map resultMap;

    protected View rootView;

    protected Button mConfirmBtn;
    protected Button mCancelBtn;
    private TextView regDate_tv;
    private TextView regType_tv;
    private TextView departmentName_tv;
    private TextView doctorName_tv;
    private TextView patientName_tv;
    private TextView idCardNo_tv;
    private TextView cost_tv;

    private OnConfirmRegFragmentInteractionListener mListener;

    public ConfirmRegFragment() {
    }

    public static ConfirmRegFragment newInstance(RegInfoForm form) {
        ConfirmRegFragment fragment = new ConfirmRegFragment();
        Bundle args = new Bundle();
        args.putString(ARG_USERID, form.getUserId());
        args.putString(ARG_DATE, form.getRegDate());
        args.putString(ARG_REGTYPEID,form.getRegTypeId());
        args.putString(ARG_REGTYPE,form.getRegType());
        args.putString(ARG_DEPTID,form.getDepartmentId());
        args.putString(ARG_DEPTNAME, form.getDepartmentName());
        args.putString(ARG_SCHDULEID,form.getScheduleId());
        args.putString(ARG_CILINICCODE,form.getClinicCode());
        args.putString(ARG_DOCTORID,form.getDoctorId());
        args.putString(ARG_CARDTYPEID, form.getCardTypeId());
        args.putString(ARG_CARDNO, form.getCardNo());
        args.putString(ARG_DOCTORNAME, form.getDoctorName());
        args.putString(ARG_COST, form.getTotalCost());
        args.putString(ARG_PATIENTID, form.getPatientId());
        args.putString(ARG_PATIENTNAME, form.getPatientName());
        args.putString(ARG_IDCARDNO, form.getIdCardNo());
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            regInfoForm = new RegInfoForm();
            regInfoForm.setUserId(getArguments().getString(ARG_USERID));
            regInfoForm.setRegDate(getArguments().getString(ARG_DATE));
            regInfoForm.setRegTypeId(getArguments().getString(ARG_REGTYPEID));
            regInfoForm.setRegType(getArguments().getString(ARG_REGTYPE));
            regInfoForm.setDepartmentId(getArguments().getString(ARG_DEPTID));
            regInfoForm.setDepartmentName(getArguments().getString(ARG_DEPTNAME));
            regInfoForm.setScheduleId(getArguments().getString(ARG_SCHDULEID));
            regInfoForm.setClinicCode(getArguments().getString(ARG_CILINICCODE));
            regInfoForm.setDoctorId(getArguments().getString(ARG_DOCTORID));
            regInfoForm.setDoctorName(getArguments().getString(ARG_DOCTORNAME));
            regInfoForm.setCardTypeId(getArguments().getString(ARG_CARDTYPEID));
            regInfoForm.setCardNo(getArguments().getString(ARG_CARDNO));
            regInfoForm.setTotalCost(getArguments().getString(ARG_COST));
            regInfoForm.setPatientId(getArguments().getString(ARG_PATIENTID));
            regInfoForm.setPatientName(getArguments().getString(ARG_PATIENTNAME));
            regInfoForm.setIdCardNo(getArguments().getString(ARG_IDCARDNO));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_confirm_reg, container, false);
        initView(rootView);
        return rootView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof DepartmentFragment.OnDeptFragmentInteractionListener) {
            mListener = (OnConfirmRegFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnOneFragmentInteractionListener");
        }
    }

    private void initView(View rootView) {
        mConfirmBtn = (Button) rootView.findViewById(R.id.confirm_reg_btn);
        mConfirmBtn.setOnClickListener(ConfirmRegFragment.this);
        mCancelBtn = (Button) rootView.findViewById(R.id.cancel_reg_btn);
        mCancelBtn.setOnClickListener(ConfirmRegFragment.this);
        regDate_tv = rootView.findViewById(R.id.regDate_tv);
        regType_tv = rootView.findViewById(R.id.regType_tv);
        departmentName_tv = rootView.findViewById(R.id.departmentName_tv);
        doctorName_tv = rootView.findViewById(R.id.doctorName_tv);
        patientName_tv = rootView.findViewById(R.id.patientName_tv);
        idCardNo_tv = rootView.findViewById(R.id.idCardNo_tv);
        cost_tv = rootView.findViewById(R.id.cost_tv);

        regDate_tv.setText("日期："+regInfoForm.getRegDate());
        regType_tv.setText("挂号类别："+regInfoForm.getRegType());
        departmentName_tv.setText("科室："+regInfoForm.getDepartmentName());
        doctorName_tv.setText("医生："+regInfoForm.getDoctorName());
        patientName_tv.setText("患者："+regInfoForm.getPatientName());
        idCardNo_tv.setText("身份证："+regInfoForm.getIdCardNo());
        cost_tv.setText("挂号费："+regInfoForm.getTotalCost()+" 元");
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.confirm_reg_btn) {
            //此处调用保存挂号信息接口
            HttpRequest.confirmReg("confirmReg",this,regInfoForm);
        }
        else if (view.getId() == R.id.cancel_reg_btn) {
            mListener.onConfirmRegFragmentInteraction("cancel",null);
        }
    }

    @Override
    public void httpClientSuccess(Object o, HttpResult httpResult) {
        JsonResult json = (JsonResult) httpResult.getResult();
        if (json == null) {
            //ToastUtils.showShort(getActivity(),"服务器处理异常");
            Tools.showDialogWithReturnMainActivity("服务器处理异常",getActivity());
            return;
        }
        if (!json.isSuccess()) {
            //ToastUtils.showShort(getActivity(),json.getMsg());
            Tools.showDialogWithReturnMainActivity(json.getMsg(),getActivity());
            return;
        }
        // 处理返回信息
        resultMap = (Map)json.getData();
        if(resultMap.get("registerId")==null){
            //ToastUtils.showShort(getActivity(),"服务器处理异常，无registerId");
            Tools.showDialogWithReturnMainActivity("服务器处理异常，无registerId",getActivity());
            return;
        }
        regInfoForm.setRegisterId(resultMap.get("registerId").toString());
        //保存成功后 跳转支付界面
        mListener.onConfirmRegFragmentInteraction("confirm",resultMap.get("registerId").toString());
    }

    @Override
    public void httpClientError(Object o, HttpResult httpResult) {
        //ToastUtils.showShort(getActivity(),"网络异常");
        Tools.showDialogWithReturnMainActivity("网络连接异常，请联系管理员！",getActivity());
    }

    public interface OnConfirmRegFragmentInteractionListener {
        void onConfirmRegFragmentInteraction(String type,String registerId);
    }
}
